package com.zlfund.cat.zjgsmk.ui.home.smk_serve;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.adapter.AdapterFactory;
import com.zlfund.cat.zjgsmk.adapter.ListViewAdapter;
import com.zlfund.cat.zjgsmk.adapter.ListViewHolder;
import com.zlfund.cat.zjgsmk.adapter.ListViewOneTypeAdapterHelper;
import com.zlfund.cat.zjgsmk.bean.HomeBean;
import com.zlfund.cat.zjgsmk.util.GlideUtil;
import com.zlfund.cat.zjgsmk.util.ViewpagerHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by chenjun on 2017/8/17.
 */

public class MainServeFragment extends Fragment {

    @BindView(R.id.vp_banner)
    ViewPager mVpBanner;
    @BindView(R.id.hot_serve1)
    RelativeLayout mHotServe1;
    @BindView(R.id.hot_serve2)
    RelativeLayout mHotServe2;
    @BindView(R.id.hot_serve3)
    RelativeLayout mHotServe3;
    @BindView(R.id.gv_smk_serve)
    GridView mGvSmkServe;
    @BindView(R.id.gv_public_serve)
    GridView mGvPublicServe;
    @BindView(R.id.gv_tripartite_serve)
    GridView mGvTripartiteServe;
    Unbinder unbinder;
    @BindView(R.id.vp_left)
    View mVpLeft;
    @BindView(R.id.vp_right)
    View mVpRight;
    @BindView(R.id.iv_hot_serve1)
    ImageView mIvHotServe1;
    @BindView(R.id.tv_hot_serve1)
    TextView mTvHotServe1;
    @BindView(R.id.iv_hot_serve2)
    ImageView mIvHotServe2;
    @BindView(R.id.tv_hot_serve2)
    TextView mTvHotServe2;
    @BindView(R.id.iv_hot_serve3)
    ImageView mIvHotServe3;
    @BindView(R.id.tv_hot_serve3)
    TextView mTvHotServe3;
    private List<HomeBean.ServiceBarBean> mBannerBeans;
    private List<HomeBean.ServiceInfoBean> mServiceBeans;
    private Context mContext;
    private List<View> mBannerViews;
    private ViewpagerHelper mViewpagerHelper;
    private AdapterFactory mFactory;
    private ListViewAdapter<HomeBean.ServiceInfoBean.ContentArrBean> mSmkAdapter;
    private ListViewAdapter<HomeBean.ServiceInfoBean.ContentArrBean> mPulicAdapter;
    private ListViewAdapter<HomeBean.ServiceInfoBean.ContentArrBean> mTriPartileAdapter;
    private List<String> mImgUrls;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_serve, container, false);
        unbinder = ButterKnife.bind(this, view);
        mViewpagerHelper = new ViewpagerHelper(mVpBanner);
        if (mImgUrls != null) mViewpagerHelper.setList(mContext,mImgUrls);
        mViewpagerHelper.addOnLeftAndRightTouchListener(mVpLeft, mVpRight);
        mFactory = AdapterFactory.singleTon();
        ListViewOneTypeAdapterHelper<HomeBean.ServiceInfoBean.ContentArrBean> helper = new ListViewOneTypeAdapterHelper<HomeBean.ServiceInfoBean.ContentArrBean>(R.layout.gv_serve_item) {
            @Override
            public void initViews(ListViewHolder holder, int position, HomeBean.ServiceInfoBean.ContentArrBean contentArrBean) {
                ImageView ivIco = holder.getView(R.id.iv_ico, ImageView.class);
                TextView tvContent = holder.getView(R.id.tv_content, TextView.class);
                tvContent.setText(contentArrBean.getTitle());
                GlideUtil.loadUrl(mContext, contentArrBean.getIco(), ivIco);
            }
        };
        mSmkAdapter = mFactory.createOneTypeLvAdapter(mContext, helper);
        mPulicAdapter = mFactory.createOneTypeLvAdapter(mContext, helper);
        mTriPartileAdapter = mFactory.createOneTypeLvAdapter(mContext, helper);
        mGvPublicServe.setAdapter(mPulicAdapter);
        mGvSmkServe.setAdapter(mSmkAdapter);
        mGvTripartiteServe.setAdapter(mTriPartileAdapter);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mViewpagerHelper.startAutoRoll();
    }

    @Override
    public void onStop() {
        super.onStop();
        mViewpagerHelper.stopAutoRoll();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        mBannerViews = new ArrayList<>();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            mViewpagerHelper.stopAutoRoll();
        }else {
            if (mBannerBeans != null && !mBannerBeans.isEmpty()) {
                mViewpagerHelper.startAutoRoll();
            }
        }
    }

    public void setServeBannerBeans(List<HomeBean.ServiceBarBean> bannerBeans) {
        if (bannerBeans == null) return;
        mBannerBeans = bannerBeans;
        if (mImgUrls == null) mImgUrls = new ArrayList<>();
        mImgUrls.clear();
        for (int i = 0; i < mBannerBeans.size(); i++) {
            mImgUrls.add(mBannerBeans.get(i).getImg());
        }
        if (mViewpagerHelper != null) {
            mViewpagerHelper.setList(mContext, mImgUrls);
            mViewpagerHelper.startAutoRoll();
        }
    }

    public void setServeInfoBeans(List<HomeBean.ServiceInfoBean> serviceBeans) {
        mServiceBeans = serviceBeans;
        initServeItem();
    }

    private void initServeItem() {
        if (mServiceBeans == null || mServiceBeans.isEmpty()) return;
        List<HomeBean.ServiceInfoBean.ContentArrBean> hotServeItems = mServiceBeans.get(0).getContentArr();
        GlideUtil.loadUrl(mContext, hotServeItems.get(0).getIco(), mIvHotServe1);
        GlideUtil.loadUrl(mContext, hotServeItems.get(1).getIco(), mIvHotServe2);
        GlideUtil.loadUrl(mContext, hotServeItems.get(2).getIco(), mIvHotServe3);
        mTvHotServe1.setText(hotServeItems.get(0).getTitle());
        mTvHotServe2.setText(hotServeItems.get(1).getTitle());
        mTvHotServe3.setText(hotServeItems.get(2).getTitle());
        mSmkAdapter.setList(mServiceBeans.get(1).getContentArr());
        mPulicAdapter.setList(mServiceBeans.get(2).getContentArr());
        mTriPartileAdapter.setList(mServiceBeans.get(3).getContentArr());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
